<?php		defined('SYSPATH') or die('No direct script access.');
class Controller_Admin_Task extends Controller_Template{

	private $_task		= '';
	private $_task_mgr	= '';
	private $_role_id	= 0;
	private $_obj_user	= NULL;

	private $_display_names	= '';
	private $_config	= '';
	private $_do_task	= '';
	private	$config		= '';
	private $_limit		= 0;
	private $_prompt	= '';

	public function __construct($request,$response){

		parent::__construct($request,$response);
		$this->_limit	= Kohana::config('task.page_size');
		//$this->_limit = 1;
		$this->config	= Kohana::config('task_config');
		$this->_config	= (array)$this->config;
		$model_name		= array_keys($this->_config);
		$this->_display_names	= $model_name[0];
		
		header("Content-Type:text/html;charset=utf-8"); 

		$this->_prompt	= new Model_WFPrompt();
		$this->_task	= new Model_WFTask();
		$this->_task_mgr= new Model_WFTaskMgr();	
		$this->_obj_user= new Model_WFUser(User::current_user_id());
		$this->_role_id	= $this->_obj_user->getDefaultRole();
		if(! $this->_role_id)
			$this->request->redirect('/user', $code = 302);
		$this->template = ('smarty:admin/task/default');
	}
	
	//待审核任务列表
	
	public function action_index($page = 1){

		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$limit	= Kohana::config('task.page_size');
		
		$offset = $limit * ($page - 1);

		//$role_id= $this->_role_id;

		$count	= $this->_task_mgr->countPreparedTasks();

		$task_info		= $this->_task_mgr->listPreparedTasks ($offset, $limit);
	
		$page_config	= array(
								'total_items'	=> $count,
								'items_per_page'=> $limit,
				);

		$pagination		= Pagination::factory($page_config);

		for($i=0;$i< count($task_info); $i++){
			
			$task_info[$i]['uname']			= User::name($task_info[$i]['uid']);
			
			$task_info[$i]['creater']		= User::name($task_info[$i]['creater']);

			$task_info[$i]['create_time']	= date('Y-m-d H:i:s',$task_info[$i]['create_time']);
			
			$task_info[$i]['type_name']		= Model_WFTask::typeName($task_info[$i]['type']);

			$task_info[$i]['status_name']	= Model_WFTask::statusName($task_info[$i]['status']);

			$task_info[$i]['desc']			= substr($task_info[$i]['description'],0,35);
		}
		$this->template->set('total', $count);
		$this->template->set('pagination', $pagination->render());
		$this->template->set('datas',$task_info);
		$this->template->set('page_title', '待审核任务列表');
		$this->response->body($this->template->render());

	}

	public function action_add()
	{
		$task_type	= $this->_config; 
		
		$this->template->set_filename('smarty:admin/task/create');

		$this->template->set('page_title' , '创建任务');
		$this->template->set('types', $task_type);
		$this->response->body($this->template->render());
	}

	protected function handle_adddata($passport, $types, $obj_id, $desc)
	{
		$type	= explode(',' , $types);
		$id				= intval($type[0]);
		$model_names	= strval($type[1]);
		
		$arr_obj_type	= Kohana::config('task_config.obj_type');
		
		if(in_array($id,$arr_obj_type['member']))
		{
			$this->_do_task	= Model::factory($model_names);
		}else{
			$this->_do_task = new $model_names ();
		}
		//判断obj_id 是否存在
		$has	= $this->_do_task->has_task($obj_id);	
		
		//获得对象信息
		$expo_info			= $this->_do_task->get_task_info($id);

		$task_info['type']	= $this->config[$model_names]['task_type'];

		$task_info['obj_id']= $obj_id;

		return $task_info;
	}

	public function action_create()
	{
		$act	= $this->request->post('action');
	
		if($act	== 'add')
		{
			$query['passport']	= strval($this->request->post('login_name'));
			$types		= strval($this->request->post('type'));
			$obj_id		= intval($this->request->post('obj_id'));
			$desc		= strval($this->request->post('desc'));
			
			if($types == 1)
				$query['site'] = 'member';
			else
				$query['site'] = 'merchant';

			$user_info	= User::search($query);
			$uid		= 0;

			if(!empty($user_info))
				$uid	= $user_info['list'][0]['uid'];
			
			if($types && intval($obj_id) > 0 && intval($uid) > 0)
			{
				
				$task_info		= $this->handle_adddata($query['passport'], $types, $obj_id, $desc);
				
				$task_info['uid']		  = $uid;

				$task_info['description'] = $desc;

				$rs				= $this->_task_mgr->submitTask($this->_obj_user, $task_info);

				if($rs)
				{	
					echo '<script>alert("添加成功!");window.location.href="/task/";</script>';
					exit;
				}else{
					
					$Mgr = $this->_task_mgr->getErrorMsg();
					echo '<script>alert(\''.$Mgr.'\');window.location.href="/task/add";</script>';
					exit;
				}
			}	
		}
		echo 'window.location.href="/task/"</script>';
	}

	//模块名   举报ID

	public function action_addone($id,$model_name = ''){

		$model_names	= (trim($model_name) == '')? $this->_display_names : $model_name;

		$id				= intval($id);
		
		$model_names	= strval($model_names);
		
		$this->_do_task	= Model::factory($model_names);

		$expo_info		= $this->_do_task->get_task_info($id);

		$task_info['uid']	= $expo_info[0]['uid'];

		$task_info['type']	= $this->config[$model_names]['task_type'];

		$task_info['obj_id']= $expo_info[0]['id'];

		$task_info['description']	= $expo_info[0]['content'];
		
		$rs				= $this->_task_mgr->submitTask($this->_obj_user, $task_info);

		if($rs)
			echo 'ok';
		else
			echo 'no';

	}
	public function action_handle(){
		
		$task_id= $_REQUEST['task_id'];
		$action	= $_REQUEST['action_id'];
		$reason	= trim($_REQUEST['reason_ee']);
		$type	= intval($this->request->post('task_type'));

		$cate_id= $this->request->post("category_sys");
		
		if(empty($cate_id) && $type != 1)
		{
			$msg    = '请正确选择分类！';
			echo '<script>alert(\''.$msg.'\');window.location.href="/task/taskdetail/'.$task_id.'/'.$type.'"</script>';
			exit;	
		}
		
		$obj_task	= new Model_WFTask($task_id);

		$obj_user	= new Model_WFUser(User::current_user_id());
		
		$rs	= $this->_task_mgr->handleTaskAction($obj_user, $obj_task, $action, $reason, $cate_id);

		if($rs)
		{
			$msg  = '处理成功!';
			echo '<script>alert(\''.$msg.'\');window.location.href="/task/list/'.$type.'"</script>';
		
		}else{

			$Mgr = $this->_task_mgr->getErrorMsg();
			
			echo '<script>alert(\''.$Mgr.'\');window.location.href="/task/list/'.$type.'";</script>';
			exit;

		}
		exit;
	}

	public function action_verify()
	{
		$task_id= $_REQUEST['task_id'];
		$action	= $_REQUEST['action_id'];
		$reason	= trim($_REQUEST['reason_ee']);

		$obj_task	= new Model_WFTask($task_id);

		$obj_user	= new Model_WFUser(User::current_user_id());

		$rs			= $this->_task_mgr->verifytask($obj_user , $obj_task , $action , $reason);

		if($rs)
			echo '<script>alert(\'审核成功\');window.location.href="/task/";</script>';
		else
		{
			$Mgr = $this->_task_mgr->getErrorMsg();
			echo '<script>alert(\''.$Mgr.'\');window.location.href="/task/";</script>';
			exit;
		}
	}

	public function action_list($type = null){

		$taskMgr	= new Model_WFTaskMgr();

		$total		= $taskMgr->CountCurrentTasks($this->_role_id);

		$rs			= $taskMgr->getCurrentTasks($this->_role_id,0,1000);
	
		for($i=0; $i < count($rs); $i++){

			$rs[$i]['start_time']	= date('Y-m-d H:i:s', $rs[$i]['start_time']);

			$rs[$i]['update_time']	= date('Y-m-d H:i:s', $rs[$i]['update_time']);
		
			//$rs[$i]['uname']		= User::name($rs[$i]['uid']);

			$rs[$i]['type_name']	= Model_WFTask::typeName($rs[$i]['type']);
			
			$rs[$i]['role_name']	= Model_WFRole::RoleName($rs[$i]['role']);

			$arr_action				= $rs[$i]['action'];

			for($k=0;$k< count($arr_action); $k++)
			{
				unset($rs[$i]['action'][$k]);	
				$rs[$i]['action'][$k]['action_id']		= $arr_action[$k];
				$rs[$i]['action'][$k]['action_name']	= Model_WFAction::getActionName($arr_action[$k]);
			}

			$prompt		= new Model_WFPrompt();
			$rs[$i]['prompt']	= $this->_prompt->getPrompt($rs[$i]['prompt_id']);

			$obj_task	= new Model_WFTask($rs[$i]['task_id']);

			$task_info	= $obj_task->getTaskInfo();

			if(!empty($task_info))
			{
				$rs[$i]['creater']	= User::name($task_info['creater']);

				$rs[$i]['uname']	= User::name($task_info['uid']);
		
				$rs[$i]['obj_id']	= $task_info['obj_id'];
		
				$rs[$i]['create_time']	= date('Y-m-d H:i:s', $task_info['create_time']);
		
				$rs[$i]['desc']		= $task_info['description'];
			}
			
		}

		if($type)
		{
			$data_rs	= '';

			for($i=0; $i< count($rs); $i++)
			{
				if($rs[$i]['type'] == $type)
					$data_rs[] = $rs[$i];
			}

			$total	= count($data_rs);	
			
			$rs	= $data_rs;
		}
		$pagin_config	= array(
								'total_items'		=> $total,
								'items_per_page'	=> $this->_limit,
							);
		$pagination		= Pagination::factory($pagin_config);
	    //print_r($rs);	
		$this->template->set_filename('smarty:admin/task/preparelist');
		$this->template->set('pagination', $pagination->render());
		$this->template->set('datas', $rs);
		$this->template->set('total',$total);
		$this->template->set('type',$type);
		$function_name  = $this->request->controller();
		$this->template->set('function_name', $function_name.$type);
		$this->template->set('page_title', '待处理任务列表');
		$this->response->body($this->template->render());
	}

	public function action_taskdetail($task_id,$task_type = null)
	{
		$data		= '';
		$rs			= $this->_task_mgr->getCurrentTasks($this->_role_id,0,1000);
		for($i=0; $i < count($rs); $i++){

			if($rs[$i]['task_id'] == $task_id)
			{
				$data['task_id']		= $task_id;
				
				$data['start_time']		= date('Y-m-d H:i:s', $rs[$i]['start_time']);

				$data['update_time']	= date('Y-m-d H:i:s', $rs[$i]['update_time']);
			
				$data['type_name']		= Model_WFTask::typeName($rs[$i]['type']);
				
				$data['role_name']		= Model_WFRole::RoleName($rs[$i]['role']);
				
				$condition['id']		= $task_id;	
				
				$task_info	= $this->_task_mgr->searchResult($condition, 0, 1);
				
				$data['type']			= $rs[$i]['type'];
				$data['obj_id']			= $task_info[0]['obj_id'];
				$arr_action				= $rs[$i]['action'];

				for($k=0;$k< count($arr_action); $k++)
				{
					unset($rs[$i]['action'][$k]);	
					$data['action'][$k]['action_id']		= $arr_action[$k];
					$data['action'][$k]['action_name']	= Model_WFAction::getActionName($arr_action[$k]);
				}

				$prompt			= new Model_WFPrompt();
				$data['prompt']	= $this->_prompt->getPrompt($rs[$i]['prompt_id']);

			}
		}
		if(empty($data))
		{
			$url	= '/task/list';

			$msg    = '任务不存在或者已处理！';
			
			echo '<script>alert(\''.$msg.'\');window.location.href="/task/list"</script>';

			exit;
		}
		$task_property_info		= '';
		if(!empty($data))
			$task_property_info	= $this->get_task_property_info($data['obj_id'], $data['type']);	
		$type = 0;
		if(!empty($data))
			$type = $data['type'];
		else
			$type = 1;
		$cate_sys = new Category_System();
		$cs_ids	  = array();
		if(! empty($task_property_info))
		{
			if(isset($task_property_info[0]['category_id']))
				$cs_ids	  = Category_system::parent_ids($task_property_info[0]['category_id']);	
		
		}
		$cate_ids	= '';
		
		for($i =0; $i < count($cs_ids); $i++)
		{
			$name = & Category_System::name($cs_ids[$i]);
			
			Category_system::commit_names();

			$cate_ids[$i]['name']	= $name;
		}
		
		$this->template->set('category_sys_1', $cate_sys->get_list());
		$this->template->set_filename('smarty:admin/task/detail');
		$this->template->set('datas', $data);
		$this->template->set('type', $type);
		$this->template->set('cate_ids', $cate_ids);
		$this->template->set('task_type', $task_type);
		$this->template->set('pro_info', $task_property_info);
		$this->template->set('task_pdata', $task_property_info);
		$this->template->set('page_title', '任务详情页');
		$this->response->body($this->template->render());
	}
	
	protected function get_task_property_info($obj_id,$type)
	{
		if(intval($obj_id) <= 0 || intval($type) <= 0)
			return null;
        $arr_obj_type   = Kohana::config('task_config.obj_type');
		
		switch ($type)
		{
			case Model_WFTask::$TYPE_EXPOSUREREPLY :
				$model_names	= 'exposure';
				break;
			case Model_WFTask::$TYPE_Category_Brand :
				$model_names	= 'Category_Brand';
				break;
			case Model_WFTask::$TYPE_NewCategory_Brand :
				$model_names	= 'Category_Brand';
				break;
			default:
				break;
		}
		if(in_array($type,$arr_obj_type['member']))
		{
		    $this->_do_task = Model::factory($model_names);
		}else{
		    $this->_do_task = new $model_names ();
		}
		
		$property_info	= $this->_do_task->get_task_info($obj_id);
		
		if($type == Model_WFTask::$TYPE_NewCategory_Brand || $type == Model_WFTask::$TYPE_Category_Brand)
		{
			for($i=0; $i<count($property_info); $i++)
			{
				$property_info[$i]['create_time']	= date('Y-m-d H:i:s', $property_info[$i]['create_time']);

				$property_info[$i]['uname']	= User::name($property_info[$i]['apply_uid']);

				$property_info[$i]['shop_name']	= Shop::name($property_info[$i]['merchant_id']);

				$domain	= Site::domain('member');

				$property_info[$i]['url']	= 'http://'.$domain.'/shop/index/'.$property_info[$i]['merchant_id'];
			
			}
		}
		return $property_info;	
	}

	public function action_loglist()
	{
	
	}
}

?>
